/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionBeans;

import entityBeans.UserEntity;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import utilities.HashFunctions;

/**
 *
 * @author h09577
 */
@Stateless
public class UserSessionBean implements UserSessionBeanLocal {

    @PersistenceContext(unitName = "forum-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    @Override
    public UserEntity register(String email, String password, String pseudo,
            String name, String firstname) {
        UserEntity newUser = new UserEntity();
        newUser.setEmail(email);
        newUser.setPassword(HashFunctions.stringToMD5(password));
        newUser.setPseudo(pseudo);
        newUser.setName(name);
        newUser.setFirstName(firstname);
        newUser.setStatus("0");

        em.persist(newUser);

        return newUser;
    }

    @Override
    public UserEntity login(String email, String password1) {
        Query q = em.createNamedQuery("UserEntity.getUsers");
        q.setParameter("email", email);
        q.setParameter("password", password1);

        List<UserEntity> user = q.getResultList(); //get a list of users having email and password specified
        if (user.size() == 1) {
            //if list contains only one user, return the user of this list
            return user.get(0);
        } else {
            return null;
        }
    }

    @Override
    public boolean testNewUserMail(String email) {
        Query q = em.createNamedQuery("UserEntity.checkEmail");
        q.setParameter("email", email);
        //if list is not empty, there already is a user using this email
        if (!q.getResultList().isEmpty()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean testNewUserPseudo(String pseudo) {
        Query q = em.createNamedQuery("UserEntity.checkPseudo");
        q.setParameter("pseudo", pseudo);
        //if list is not empty, there already is a user using this pseudo
        if (!q.getResultList().isEmpty()) {
            return true;
        }
        return false;
    }
    
    @Override
    public UserEntity editMyAccount(UserEntity userConnected, 
    String pseudo, String name,String firstname) {
        userConnected.setPseudo(pseudo);
        userConnected.setName(name);
        userConnected.setFirstName(firstname);
        em.merge(userConnected);
        return userConnected;
    }

    @Override
    public boolean testNewUserPseudoUpdate(Long id, String pseudo) {
        Query q = em.createNamedQuery("UserEntity.checkUpdatePseudo");
        q.setParameter("pseudo", pseudo);
        q.setParameter("id", id);
        //if list is not empty, there already is another user than the user logged who use this pseudo
        if (q.getResultList().isEmpty()) {
            return true;
        }
        return false;
    }

    @Override
    public UserEntity editMyPassword(UserEntity userConnected, String password1) {
        userConnected.setPassword(HashFunctions.stringToMD5(password1));
        em.merge(userConnected);
        return userConnected;
    }

    @Override
    public UserEntity editMySignature(UserEntity userConnected, String signature) {
        userConnected.setSignature(signature);
        em.merge(userConnected);
        return userConnected;
    }
}
